package com.quiz.bis.controller;

import com.quiz.bis.domain.bo.PaperBO;
import com.quiz.bis.domain.model.PaperQuery;
import com.quiz.bis.domain.model.PreviewOptions;
import com.quiz.bis.domain.vo.PaperListVO;
import com.quiz.bis.domain.vo.PaperPreviewVO;
import com.quiz.bis.domain.vo.PaperVO;
import com.quiz.bis.service.IPaperService;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 试卷管理v2
 *
 * @author ahuan
 */
@RestController
@RequestMapping("paper2")
@Tag(name = "试卷管理v2")
@RequiredArgsConstructor
public class PaperController extends BaseController {

    private final IPaperService paperService;

    /**
     * 分页查询试卷列表
     *
     * @param query
     * @param pageQuery
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<PaperListVO> list(PaperQuery query, PageQuery pageQuery) {
        return paperService.pagePaperList(pageQuery, query);
    }

    /**
     * 批量更新试卷状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PutMapping("/status")
    public R<Void> updateStatus(@RequestParam List<Long> ids, @RequestParam Integer status) {
        paperService.updateStatusBatch(ids, status);
        return R.ok();
    }

    /**
     * 批量删除试卷
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<Void> delete(@RequestParam List<Long> ids) {
        paperService.deletePaper(ids);
        return R.ok();
    }

    /**
     * 恢复试卷
     * @param ids
     * @return
     */
    @PutMapping("/recover")
    public R<Void> recover(@RequestParam List<Long> ids) {
        paperService.recoverPaper(ids);
        return R.ok();
    }

    /**
     * 创建试卷
     *
     * @return {@link R}<{@link Long}> 试卷ID
     * @author ahuan
     */
    @PostMapping
    public R<Long> create(@Validated @RequestBody PaperBO paperBO) {
        return paperService.createPaper(paperBO);
    }

    /**
     * 获取试卷详情
     *
     * @return {@link R}<{@link PaperVO}> 试卷详情
     * @author ahuan
     */
    @GetMapping("{paperId}")
    public R<PaperVO> detail(@PathVariable Long paperId) {

        R<PaperVO> paperDetail = paperService.getPaperDetail(paperId);

        return paperDetail;
    }

    /**
     * 更新试卷
     *
     * @return {@link R}<{@link Void}> 更新结果
     * @author ahuan
     */
    @PutMapping
    public R<Void> update(@Validated @RequestBody PaperBO paperBO) {
        return paperService.updatePaper(paperBO);
    }

    /**
     * 删除试卷
     *
     * @return {@link R}<{@link Void}> 删除结果
     * @author ahuan
     */
    @DeleteMapping("{paperId}")
    public R<Void> delete(@PathVariable Long paperId) {
        return paperService.deletePaper(paperId);
    }

    /**
     * 预览试卷
     *
     * @param paperId             试卷ID
     * @param showAnswers         是否显示答案
     * @param showOptionAnswers   是否显示选项答案
     * @param showAnalysis        是否显示解析
     * @param showKnowledgePoints 是否显示知识点
     * @param showDifficulty      是否显示难度
     * @return {@link R}<{@link PaperPreviewVO}> 预览数据
     */
    @GetMapping("preview/{paperId}")
    public R<PaperPreviewVO> preview(
            @PathVariable Long paperId,
            @RequestParam(required = false, defaultValue = "true") Boolean showAnswers,
            @RequestParam(required = false, defaultValue = "true") Boolean showOptionAnswers,
            @RequestParam(required = false, defaultValue = "true") Boolean showAnalysis,
            @RequestParam(required = false, defaultValue = "true") Boolean showKnowledgePoints,
            @RequestParam(required = false, defaultValue = "true") Boolean showDifficulty) {

        PreviewOptions options = new PreviewOptions();
        options.setShowAnswers(showAnswers);
        options.setShowOptionAnswers(showOptionAnswers);
        options.setShowAnalysis(showAnalysis);
        options.setShowKnowledgePoints(showKnowledgePoints);
        options.setShowDifficulty(showDifficulty);

        return paperService.previewPaper(paperId, options);
    }

    /**
     * 导出试卷
     *
     * @param paperId             试卷ID
     * @param exportType          导出类型(pdf/word)
     * @param showAnswers         是否显示答案
     * @param showOptionAnswers   是否显示选项答案
     * @param showAnalysis        是否显示解析
     * @param showKnowledgePoints 是否显示知识点
     * @param showDifficulty      是否显示难度
     * @param response            HTTP响应
     */
    @GetMapping("export/{paperId}")
    public void export(
            @PathVariable Long paperId,
            @RequestParam String exportType,
            @RequestParam(required = false, defaultValue = "true") Boolean showAnswers,
            @RequestParam(required = false, defaultValue = "true") Boolean showOptionAnswers,
            @RequestParam(required = false, defaultValue = "true") Boolean showAnalysis,
            @RequestParam(required = false, defaultValue = "true") Boolean showKnowledgePoints,
            @RequestParam(required = false, defaultValue = "true") Boolean showDifficulty,
            HttpServletResponse response, HttpServletRequest request) {

        // 构建预览选项
        PreviewOptions options = new PreviewOptions();
        options.setShowAnswers(showAnswers);
        options.setShowOptionAnswers(showOptionAnswers);
        options.setShowAnalysis(showAnalysis);
        options.setShowKnowledgePoints(showKnowledgePoints);
        options.setShowDifficulty(showDifficulty);

        paperService.exportPaper(paperId, exportType, options, response, request);
    }
}